package com.qf.mapper;

import com.qf.model.Farm;
import com.qf.model.User;
import org.apache.ibatis.annotations.*;
import org.apache.ibatis.mapping.FetchType;

import java.util.List;


@Mapper
public interface FarmMapper {

//    @ResultMap("farmMap")
    @Select("select * from farm")
    List<Farm> selectAll();

    @Results(id = "farmMap",value = {
                @Result(column = "id",property = "id"),
                @Result(column = "province",property = "province"),
                @Result(column = "city",property = "city"),
                @Result(column = "district",property = "district"),
                @Result(column = "type",property = "type"),
                @Result(column = "name",property = "name"),
                @Result(column = "linkman",property = "linkman"),
                @Result(column = "telephone",property = "telephone"),
                @Result(column = "createtime",property = "createtime"),
                @Result(column = "updatetime",property = "updatetime"),
                @Result(column = "userId",property = "userId"),
                @Result(column = "mainCrop",property = "mainCrop"),
                @Result(column = "userId",property ="user",javaType = User.class,one=
                @One(select = "com.qf.mapper.UserMapper.findById" ,fetchType = FetchType.EAGER)
            )
})
    @Select("select * from farm limit #{currentPage},#{pageSize}")
    List<Farm> findPage(@Param("currentPage")Integer currentPage, @Param("pageSize")Integer pageSize);

    @Select("select count(*) from farm")
    Long count();

    @Insert("insert into farm (province,city,district,type,name,linkman,telephone,createtime,updatetime,userId,mainCrop) values (#{province},#{city},#{district},#{type},#{name},#{linkman},#{telephone},#{createtime},#{updatetime},#{userId},#{mainCrop})")
    int insertFarm(Farm farm);

    @ResultMap("farmMap")
    @Select("select * from farm where id =#{id}")
    Farm findFarm(int id);

    @Update("update farm set province=#{province},city=#{city},district=#{district},type=#{type},name=#{name},linkman=#{linkman},userId=#{userId},telephone=#{telephone},createtime=#{createtime},updatetime=#{updatetime},userId=#{userId},mainCrop=#{mainCrop}" +
            "where id=#{id}")
    int edit(Farm farm);

    @Delete("delete from farm where id =#{id}")
    int delFarm(Integer id);

}
